﻿namespace QLPhongKham.Access.VienPhi
{
    using ConfigDatabase.Class;
    using Npgsql;
    using QLPhongKham.Access;
    using QLPhongKham.BaseClass.BenhNhan;
    using QLPhongKham.BaseClass.Chung;
    using QLPhongKham.BaseClass.Duoc;
    using QLPhongKham.BaseClass.VienPhi;
    using System;
    using System.Data;

    public class VThuTien
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _conn;
        private General _xuli;

        public VThuTien()
        {
            this._condata = new ConnectDatabase();
            this._xuli = new General(this._condata);
        }

        public VThuTien(ConnectDatabase condata)
        {
            this._condata = condata;
            this._xuli = new General(this._condata);
        }

        public long f_get_capsophieu(int loaiphieu)
        {
            long num = 0L;
            string str = "";
            if (loaiphieu == 1)
            {
                str = " and " + dtovpThanhToanVP.columnIDQL + ">0";
            }
            else if (loaiphieu == 2)
            {
                str = " and " + dtovpThanhToanVP.columnIDQL + "=0";
            }
            try
            {
                num = long.Parse(this._xuli.f_Get_dulieu("select max(" + dtovpThanhToanVP.columnSOPHIEU + ") as sophieu  from " + this._xuli.pSchema + "." + dtovpThanhToanVP.tableName + " where to_char(" + dtovpThanhToanVP.columnNGAYTHU + ",'yyyy')='" + DateTime.Now.ToString("yyyy") + "'" + str).Tables[0].Rows[0][0].ToString());
            }
            catch
            {
            }
            return (num + 1L);
        }

        public bool f_hoantra_thanhtoan(dtovpThanhToanVP ttvp)
        {
            bool flag;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                DataTable table = this._xuli.f_Get_dulieu(string.Concat(new object[] { "select ", dtovpThanhToanVPCT.columnID, " from ", this._xuli.pSchema, ".", dtovpThanhToanVPCT.tableName, " where ", dtovpThanhToanVPCT.columnIDTTVP, "=", ttvp.pID })).Tables[0];
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                int num = 0;
                num = 0;
                while (num < table.Rows.Count)
                {
                    dtovpThanhToanVPCT nvpct = new dtovpThanhToanVPCT(this._condata) {
                        pID = long.Parse(table.Rows[num][0].ToString())
                    };
                    nvpct.Load();
                    if (nvpct.pIDCHIDINH > 0M)
                    {
                        if (nvpct.pLOAINOIDUNG == HangSo.LoaiNoiDungVienPhi.enLoaiNoiDungVP.DichVu.GetHashCode())
                        {
                            dtoBNChiDinh dinh = new dtoBNChiDinh(this._condata) {
                                pID = nvpct.pIDCHIDINH
                            };
                            dinh.Load();
                            dinh.pSLDATHANHTOAN -= nvpct.pSOLUONGTHU;
                            if (!dinh.Update(this._conn))
                            {
                                break;
                            }
                        }
                        else
                        {
                            dtoDToaThuocCT cct = new dtoDToaThuocCT(this._condata) {
                                pID = long.Parse(nvpct.pIDCHIDINH.ToString())
                            };
                            cct.Load();
                            cct.pSLDATHANHTOAN -= nvpct.pSOLUONGTHU;
                            if (!cct.Update(this._conn))
                            {
                                break;
                            }
                        }
                    }
                    num++;
                }
                ttvp.pHOANTRA = 1;
                if ((num == table.Rows.Count) && ttvp.Update(this._conn))
                {
                    transaction.Commit();
                    return true;
                }
                transaction.Rollback();
                flag = false;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return flag;
        }

        public bool f_huyphieunhap(long id)
        {
            int num = -1;
            try
            {
                NpgsqlConnection connect = new NpgsqlConnection(this._condata.StringConnect);
                connect.Open();
                NpgsqlTransaction transaction = connect.BeginTransaction();
                try
                {
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDNhapKhoCT.tableName, " where ", dtoDNhapKhoCT.columnIDNHAPKHO, "=", id }), connect);
                    num = -1;
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDNhapKho.tableName, " where ", dtoDNhapKho.columnID, "=", id }), connect);
                    if (num > -1)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                catch
                {
                    transaction.Rollback();
                }
                finally
                {
                    connect.Close();
                    connect.Dispose();
                }
            }
            catch
            {
            }
            return (num > -1);
        }

        public long f_insert_thanhtoan(dtovpThanhToanVP ttvp, dtovpThanhToanVPCT[] chitiet)
        {
            long num2;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                ttvp.pID = long.Parse(this._xuli.f_Get_CapID());
                if (!ttvp.Insert(this._conn))
                {
                    return 0L;
                }
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                int index = 0;
                index = 0;
                while (index < chitiet.Length)
                {
                    chitiet[index].pIDTTVP = ttvp.pID;
                    chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                    if (!chitiet[index].Insert(this._conn))
                    {
                        chitiet[index].pID = 0L;
                        transaction.Rollback();
                        break;
                    }
                    if (chitiet[index].pLOAINOIDUNG == HangSo.LoaiNoiDungVienPhi.enLoaiNoiDungVP.DichVu.GetHashCode())
                    {
                        if (this._xuli.f_Update_dulieu(string.Concat(new object[] { "update ", this._xuli.pSchema, ".", dtoBNChiDinh.tableName, " set ", dtoBNChiDinh.columnSLDATHANHTOAN, "=", dtoBNChiDinh.columnSLDATHANHTOAN, "+", chitiet[index].pSOLUONGTHU, " where ", dtoBNChiDinh.columnID, "=", chitiet[index].pIDCHIDINH }), this._conn) == -1)
                        {
                            chitiet[index].pID = 0L;
                            break;
                        }
                    }
                    else if (this._xuli.f_Update_dulieu(string.Concat(new object[] { "update ", this._xuli.pSchema, ".", dtoDToaThuocCT.tableName, " set ", dtoDToaThuocCT.columnSLDATHANHTOAN, "=", dtoDToaThuocCT.columnSLDATHANHTOAN, "+", chitiet[index].pSOLUONGTHU, " where ", dtoDToaThuocCT.columnID, "=", chitiet[index].pIDCHIDINH }), this._conn) == -1)
                    {
                        chitiet[index].pID = 0L;
                        break;
                    }
                    index++;
                }
                if (index == chitiet.Length)
                {
                    if (ttvp.pSOBIENLAI > 0)
                    {
                        this._xuli.f_Update_dulieu(string.Concat(new object[] { "update ", this._xuli.pSchema, ".", dtovpDMQuyenSo.tableName, " set ", dtovpDMQuyenSo.columnSODANGGHI, "=", dtovpDMQuyenSo.columnSODANGGHI, " where ", dtovpDMQuyenSo.columnID, "=", ttvp.pIDQUYENSO }));
                    }
                    transaction.Commit();
                    return ttvp.pID;
                }
                transaction.Rollback();
                ttvp.Delete();
                num2 = 0L;
            }
            catch
            {
                num2 = 0L;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num2;
        }

        public long f_insert_thutien(dtovpThanhToanVP ttvp, dtovpThanhToanVPCT[] chitiet)
        {
            long num2;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                ttvp.pID = long.Parse(this._xuli.f_Get_CapID());
                if (!ttvp.Insert(this._conn))
                {
                    return 0L;
                }
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                int index = 0;
                index = 0;
                while (index < chitiet.Length)
                {
                    chitiet[index].pIDTTVP = ttvp.pID;
                    if (chitiet[index].pID != 0L)
                    {
                        if (!chitiet[index].Update(this._conn))
                        {
                            transaction.Rollback();
                            break;
                        }
                    }
                    else
                    {
                        chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                        if (!chitiet[index].Insert(this._conn))
                        {
                            chitiet[index].pID = 0L;
                            transaction.Rollback();
                            break;
                        }
                    }
                    index++;
                }
                if (index == chitiet.Length)
                {
                    transaction.Commit();
                    return ttvp.pID;
                }
                ttvp.Delete();
                num2 = 0L;
            }
            catch
            {
                num2 = 0L;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num2;
        }

        public DataTable f_loaddata_thutien(long id, string tungay, string denngay, int loaithutien, bool hoantra, int idquyenso)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = "select a." + dtovpThanhToanVP.columnID + " as id,a." + dtovpThanhToanVP.columnSOPHIEU + " as sophieu,a." + dtovpThanhToanVP.columnHOADON + " as hoadon,to_char(a." + dtovpThanhToanVP.columnNGAYTHU + ",'dd/mm/yyyy hh24:mi') as ngay,a." + dtovpThanhToanVP.columnIDDANGNHAP + " as iddangnhap,b." + dtoDMDangNhap.columnTENNGUOIDUNG + " as tennguoidangnhap,a." + dtovpThanhToanVP.columnHOTEN + " as hoten,a." + dtovpThanhToanVP.columnNAMSINH + " as namsinh,a." + dtovpThanhToanVP.columnIDGIOITINH + " as idgioitinh,a." + dtovpThanhToanVP.columnDIACHI + " as diachi,a." + dtovpThanhToanVP.columnGHICHU + " as ghichu,a." + dtovpThanhToanVP.columnTONGTIEN + " as tongcong,a." + dtovpThanhToanVP.columnTHUCTHU + " as thucthu,a." + dtovpThanhToanVP.columnIDQL + " as idql,a2." + dtovpDMQuyenSo.columnKIHIEU + " as kihieuquyenso,a2." + dtovpDMQuyenSo.columnTEN + " as tenquyenso,a2." + dtovpDMQuyenSo.columnID + " as idquyenso from " + str + dtovpThanhToanVP.tableName + " a left join " + str + dtoDMDangNhap.tableName + " b on a." + dtovpThanhToanVP.columnIDDANGNHAP + "=b." + dtoDMDangNhap.columnID + " left join " + str + dtovpDMQuyenSo.tableName + " a2 on a." + dtovpThanhToanVP.columnIDQUYENSO + "=a2." + dtovpDMQuyenSo.columnID + " where 1=1 " + ((id == 0L) ? "" : string.Concat(new object[] { " and a.", dtovpThanhToanVP.columnID, "=", id })) + ((tungay == "") ? "" : (" and to_timestamp(to_char(a." + dtovpThanhToanVP.columnNGAYTHU + ",'dd/mm/yyyy'),'dd/mm/yyyy')>=to_timestamp('" + tungay + "','dd/mm/yyyy')")) + ((denngay == "") ? "" : (" and to_timestamp(to_char(a." + dtovpThanhToanVP.columnNGAYTHU + ",'dd/mm/yyyy'),'dd/mm/yyyy')<=to_timestamp('" + denngay + "','dd/mm/yyyy')")) + (!hoantra ? (" and " + dtovpThanhToanVP.columnHOANTRA + "=0") : "") + ((idquyenso != 0) ? string.Concat(new object[] { " and a.", dtovpThanhToanVP.columnIDQUYENSO, "=", idquyenso }) : "");
            if (loaithutien == 1)
            {
                sql = sql + " and a." + dtovpThanhToanVP.columnIDQL + ">0";
            }
            else if (loaithutien == 2)
            {
                sql = sql + " and a." + dtovpThanhToanVP.columnIDQL + "=0";
            }
            sql = sql + " order by a." + dtovpThanhToanVP.columnNGAYTHU + " desc";
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public DataTable f_loaddata_thutienct(long idttvp)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = string.Concat(new object[] { 
                "select b.", dtovpThanhToanVPCT.columnIDTTVP, " as idttvp,b.", dtovpThanhToanVPCT.columnID, " as id,b.", dtovpThanhToanVPCT.columnIDCHIDINH, " as idchidinh,b.", dtovpThanhToanVPCT.columnIDNOIDUNG, " as idvp,b.", dtovpThanhToanVPCT.columnTENNOIDUNG, " as tenvp,b.", dtovpThanhToanVPCT.columnMANOIDUNG, " as mavp,b.", dtovpThanhToanVPCT.columnDVT, " as dvt,case when b.", dtovpThanhToanVPCT.columnLOAINOIDUNG, 
                "=", HangSo.LoaiNoiDungVienPhi.enLoaiNoiDungVP.Thuoc.GetHashCode(), " then 1 else 0 end  as thuoc,b.", dtovpThanhToanVPCT.columnSOLUONGTHU, " as soluong,b.", dtovpThanhToanVPCT.columnDONGIA, " as dongia,b.", dtovpThanhToanVPCT.columnSOLUONGTHU, " *b.", dtovpThanhToanVPCT.columnDONGIA, " as thanhtien,b.", dtovpThanhToanVPCT.columnTHUCTHU, " as thucthu from ", str, dtovpThanhToanVP.tableName, " a inner join ", 
                str, dtovpThanhToanVPCT.tableName, " b on a.", dtovpThanhToanVP.columnID, "=b.", dtovpThanhToanVPCT.columnIDTTVP, " where a.", dtovpThanhToanVP.columnID, "=", idttvp, " order by a.", dtovpThanhToanVP.columnNGAYTHU, " desc"
             });
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public decimal f_update_thutien(dtovpThanhToanVP ToaThuoc, dtovpThanhToanVPCT[] chitiet)
        {
            decimal num3;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            NpgsqlTransaction transaction = this._conn.BeginTransaction();
            try
            {
                dtovpThanhToanVPCT nvpct;
                dtoBNChiDinh dinh;
                dtoDToaThuocCT cct;
                if (!ToaThuoc.Update(this._conn))
                {
                    transaction.Rollback();
                    return 0M;
                }
                string str = "";
                int index = 0;
                index = 0;
                while (index < chitiet.Length)
                {
                    if (chitiet[index].pID != 0L)
                    {
                        if (chitiet[index].pIDCHIDINH > 0M)
                        {
                            nvpct = new dtovpThanhToanVPCT(this._condata) {
                                pID = chitiet[index].pID
                            };
                            nvpct.Load();
                            if (nvpct.pLOAINOIDUNG == HangSo.LoaiNoiDungVienPhi.enLoaiNoiDungVP.DichVu.GetHashCode())
                            {
                                dinh = new dtoBNChiDinh(this._condata) {
                                    pID = nvpct.pIDCHIDINH
                                };
                                dinh.Load();
                                dinh.pSLDATHANHTOAN = (dinh.pSLDATHANHTOAN - nvpct.pSOLUONGTHU) + chitiet[index].pSOLUONGTHU;
                                if (!dinh.Update(this._conn))
                                {
                                    break;
                                }
                            }
                            else
                            {
                                cct = new dtoDToaThuocCT(this._condata) {
                                    pID = long.Parse(nvpct.pIDCHIDINH.ToString())
                                };
                                cct.Load();
                                cct.pSLDATHANHTOAN = (cct.pSLDATHANHTOAN - nvpct.pSOLUONGTHU) + chitiet[index].pSOLUONGTHU;
                                if (!cct.Update(this._conn))
                                {
                                    break;
                                }
                            }
                        }
                        if (!chitiet[index].Update(this._conn))
                        {
                            break;
                        }
                    }
                    else
                    {
                        chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                        if (!chitiet[index].Insert(this._conn))
                        {
                            break;
                        }
                    }
                    str = str + chitiet[index].pID + ",";
                    nvpct = null;
                    index++;
                }
                if (index == chitiet.Length)
                {
                    str = str.Trim(new char[] { ',' });
                    DataTable table = this._xuli.f_Get_dulieu(string.Concat(new object[] { "select ", dtovpThanhToanVPCT.columnID, " from ", this._xuli.pSchema, ".", dtovpThanhToanVPCT.tableName, " where ", dtovpThanhToanVPCT.columnIDTTVP, "=", ToaThuoc.pID, " and ", dtovpThanhToanVPCT.columnID, " not in(", str, ")" })).Tables[0];
                    int num2 = 0;
                    num2 = 0;
                    while (num2 < table.Rows.Count)
                    {
                        nvpct = new dtovpThanhToanVPCT(this._condata) {
                            pID = long.Parse(table.Rows[num2][0].ToString())
                        };
                        nvpct.Load();
                        if (nvpct.pIDCHIDINH > 0M)
                        {
                            if (nvpct.pLOAINOIDUNG == HangSo.LoaiNoiDungVienPhi.enLoaiNoiDungVP.DichVu.GetHashCode())
                            {
                                dinh = new dtoBNChiDinh(this._condata) {
                                    pID = nvpct.pIDCHIDINH
                                };
                                dinh.Load();
                                dinh.pSLDATHANHTOAN -= nvpct.pSOLUONGTHU;
                                if (!dinh.Update(this._conn))
                                {
                                    break;
                                }
                            }
                            else
                            {
                                cct = new dtoDToaThuocCT(this._condata) {
                                    pID = long.Parse(nvpct.pIDCHIDINH.ToString())
                                };
                                cct.Load();
                                cct.pSLDATHANHTOAN -= nvpct.pSOLUONGTHU;
                                if (!cct.Update(this._conn))
                                {
                                    break;
                                }
                            }
                        }
                        if (!nvpct.Delete(this._conn))
                        {
                            break;
                        }
                        num2++;
                    }
                    if (num2 == table.Rows.Count)
                    {
                        transaction.Commit();
                        return ToaThuoc.pID;
                    }
                    transaction.Rollback();
                    return 0M;
                }
                transaction.Rollback();
                num3 = 0M;
            }
            catch
            {
                transaction.Rollback();
                num3 = 0M;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num3;
        }
    }
}

